package com.cn.parallel.task;

import java.util.concurrent.Callable;

public abstract class AbstractTask<T> implements Callable<T> {
	private Lock lock = null;

	void setLock(Lock lock) {
		this.lock = lock;
	}

	public T call() throws Exception {
		T result = null;
		try {
			result = this.execute();// 执行子类具体任务
		} catch (Exception e) {
			e.printStackTrace();
		}
		synchronized (lock) {
			// 处理完业务后，任务结束，递减线程数，同时唤醒主线程
			lock.thread_count--;
			lock.notifyAll();
		}
		return result;
	}

	/**
	 * 业务处理函数
	 */
	public abstract T execute() throws Exception;
}
